<template>
  <!--物资管理/成品入库页面-->
  <div>
    <cus-search style="min-width: 1200px">
      <cus-search-item>
        <Form :label-width="90">
          <FormItem label="起止日期" class="mr_24 mb_24">
           <DatePicker
                class="itemImport"
                type="daterange"
                placeholder="开始日期 - 结束日期"
                @on-change="daterangeOneChange"
                style="width: 200px"
              />
          </FormItem>
          <FormItem label="采购单号" class="mr_24 mb_24">
            <Input
              clearable
              placeholder="请输入采购单号"
              v-model="form.billNo"
            />
          </FormItem>
          <cusButton
            class="mr_10 mb_20"
            type="primary"
            v-permission="10"
            @onClick="getDishStockListApi()"
            >查询</cusButton
          >
          <cusButton
            class="mr_10 mb_20"
            type="primary"
            v-permission="10"
            @onClick="getDishStockPullWeighRecordsApi()"
            >拉取称重数据</cusButton
          >
        </Form>
      </cus-search-item>
    </cus-search>
    <cus-allcard style="margin-top: 24px; padding-top: 24px" :showTitle="false">
      <Table
        border
        :loading="form.loading"
        :columns="columns"
        :data="data"
        :size="vuex_config.tableSize"
        :height="tableHeight"
      >
        <template slot-scope="{ row }" slot="voucherOperation">
          <div class="flex" v-if="row.voucher">
            <cus-button
              type="text"
              size="small"
              :style="{ color: '#1E6FFF' }"
              @onClick="voucherShow(row)"
            >
              查看
            </cus-button>
          </div>
        </template>
        <template slot-scope="{ row }" slot="operation">
          <div class="flex">
            <cus-button
              type="text"
              size="small"
              :style="{ color: '#1E6FFF' }"
              style="padding: 0 5px"
              @onClick="onShow(row)"
            >
              <Icon type="ios-search" size="4" style="margin: 2px 3px 0 0" />
              <span>查看</span>
            </cus-button>
            <cus-button
              type="text"
              size="small"
              :style="{ color: '#1E6FFF' }"
              style="padding: 0 5px"
              @onClick="onTableExport(row)"
              ><Icon
                type="ios-download-outline"
                size="4"
                style="margin: 2px 3px 0 0"
              />
              <span>导出</span>
            </cus-button>
            <cus-button
              type="text"
              size="small"
              v-if="row.checkStatus == '未确认'"
              :style="{ color: '#1E6FFF' }"
              style="padding: 0 5px"
              @onClick="onConfirm(row)"
            >
              <Icon
                type="ios-checkbox-outline"
                size="4"
                style="margin: 2px 3px 0 0"
              />
              <span>确认</span>
            </cus-button>
          </div>
        </template>
      </Table>
      <div class="mt_24 cus_page">
        <cus-page
          :data="pages"
          class="mt_24"
          @pageChange="getDishStockListApi"
        ></cus-page>
      </div>
    </cus-allcard>
  </div>
</template>
<script>
// import {
//   getDishStockList,
//   getDishStockPullWeighRecords,
//   selectBasstockGetAllStockTypes,
//   postGeneratePoStockbillIportExcel,
//   postApiGoodsHandaworkCheckstockbill
// } from "@/api/business/manager";
const columns = [
  {
    title: "序号",
    type: "index",
    align: "center",
    minWidth: 70,
  },
  {
    title: "采购单号",
    key: "billNo",
    minWidth: 175,
    align: "center",
  },
  {
    title: "加工任务日期",
    key: "validDate",
    minWidth: 135,
    align: "center",
  },
  {
    title: "入库方式",
    key: "storageway",
    minWidth: 135,
    align: "center",
  },
  {
    title: "状态",
    key: "checkStatus",
    minWidth: 135,
    align: "center",
  },
  {
    title: "最新验收时间",
    key: "updateTime",
    minWidth: 175,
    align: "center",
  },
  {
    title: "凭证",
    slot: "voucherOperation",
    key: "voucher",
    minWidth: 135,
    align: "center",
  },
  {
    title: "操作",
    fixed: "right", // 右侧固定
    slot: "operation",
    align: "center",
    minWidth: 175,
  },
];
export default {
  components: {},
  data() {
    return {
      form: {
        loading: false,
        billNo:"",//采购单号
        stockType: "",//仓库类型。goods：物资库。product：产品库
        startDate: "",//最新验收的开始时间，包含。格式：yyyy-MM-dd
        stopDate: "",//最新验收的结束时间，包含。格式：yyyy-MM-dd
      },
      ruleValidate: {}, //rule验证
      stockTypeTree:[],//仓库列表select数据
      columns: columns,// 表头
      data: [], // 表格列表
      // 分页
      pages: {
        page: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      tableHeight: 0,
    };
  },
  created() {
    this.onInit();
  },
  mounted() {
    setTimeout(() => {
      this.tableHeight = window.innerHeight - 370; // 表格自适应高度
    }, 100);
    window.addEventListener("resize", this.tableHeightChange, true);
  },
  destroyed() {
    window.removeEventListener("resize", this.tableHeightChange, true);
  },
  methods: {
    tableHeightChange() {
      this.tableHeight = 0;
      setTimeout(() => {
        this.tableHeight = window.innerHeight - 370; // 表格自适应高度
      }, 100);
    },
    onInit() {
      this.getDishStockListApi();//列表接口
      this.selectBasstockGetAllStockTypesApi();//仓库列表
    },
    //成品入库-起止日期事件
    daterangeOneChange(v) {
      this.form.startDate = v[0];
      this.form.stopDate = v[1];
      this.getDishStockListApi();
    },
    //查看凭证
    voucherShow(v) {
      window.location.href = v.voucher;
    },
    //查看详情
    onShow(row) {
      console.log(row);
      this.$router.push({
        path: "/wrehousingDetails/wrehousingDetails",
        query: {
          billNo: row.billNo, //采购单号
          //checkName: row.checkName, //验收员
          //supplierName: row.supplierName, //供应商
          createTime: row.createTime, //日期
          storageway: row.storageway, //入库方式
          checkStatus: row.checkStatus, //状态,判断详情是否可修改
          isEdit:'false'//状态false为成品入库查看详情不需要供应商,验收员,添加明细,编辑操作
        },
      });
    },
     //导出excel报表数据
    onTableExport(row) {
      this.postGeneratePoStockbillIportExcelApi(row.id);
      console.log(row.id);
    },
    //确认审核table单
    onConfirm(row) {
      this.postApiGoodsHandaworkCheckstockbillApi(row.id);
    },
    //成品入库验收列表---------------接口
    getDishStockListApi() {
      this.form.loading = true;
      return new Promise((resolve) => {
        let params = {
          billNo:this.form.billNo,//采购单号
          stockType: this.form.stockType,//仓库类型。goods：物资库。product：产品库
          startDate: this.form.startDate,//最新验收的开始时间，包含。格式：yyyy-MM-dd
          stopDate:this.form.stopDate,//最新验收的结束时间，包含。格式：yyyy-MM-dd
          current: this.pages.page,
          size: this.pages.pageSize,
        };
        getDishStockList(params).then((res) => {
          this.form.loading = false;
          if (res.retCode == 2000) {
            res.data.records.forEach((element) => {
              //入库方式 0 （电子秤采购入库） 1（手工入库）2（电子秤无单入库）3（冲销）4（补缴）5（电子秤采购越库）6（电子秤无单越库）7 (扫描配送单二维码生成)
              element.storageway =
                element.storageway == 0
                  ? "电子秤采购入库"
                  : element.storageway == 1
                  ? "手工入库"
                  : element.storageway == 2
                  ? "电子秤无单入库"
                  : element.storageway == 3
                  ? "冲销"
                  : element.storageway == 4
                  ? "补缴"
                  : element.storageway == 5
                  ? "电子秤采购越库"
                  : element.storageway == 6
                  ? "电子秤无单越库"
                  : element.storageway == 7
                  ? "扫描配送单二维码生成" 
                  :element.storageway == 8
                  ? "采购单入库" 
                  :element.storageway == 9
                  ? "成品入库"
                  :"";
              element.checkStatus =
                element.checkStatus == 0 ? "已确认" : "未确认"; //0：已确认，1：未确认
            });
            this.data = res.data.records;
            this.pages.pageTotal = res.data.total;
          } 
          resolve();
        });
      });
    },
    //拉取桌面称今日的称重数据------------------接口
    getDishStockPullWeighRecordsApi() {
      this.$cu.showLoading();
      return new Promise((resolve) => {
        getDishStockPullWeighRecords().then((res) => {
           this.$cu.hideLoading(1.5);
          if (res.retCode == 2000) {
            this.$Message.success(res.retMsg);
            this.getDishStockListApi();//刷新列表
            resolve();
          } 
        });
      });
    },
    //获取所有的仓库类型-----------------接口
    selectBasstockGetAllStockTypesApi() {
      this.stockTypeTree = [];
      return new Promise((resolve) => {
        selectBasstockGetAllStockTypes().then((res) => {
          if (res.retCode == 2000) {
            //遍历对象
            Object.keys(res.data).forEach((key) => {
              let obj = {
                label: res.data[key],
                value: key,
              };
              this.stockTypeTree.push(obj);
              console.log(key);
            });
            resolve();
          } 
        });
      });
    },
    //物资入库验收Table列表采购单导出------------------接口
    postGeneratePoStockbillIportExcelApi(id) {
      this.$cu.showLoading();
      return new Promise((resolve) => {
        postGeneratePoStockbillIportExcel({ stockId: id }).then(
          (res) => {
            this.$cu.hideLoading();
            if (res.retCode === 2000) {
              this.$Message.success(res.retMsg);
              window.location.href = res.data;
              resolve();
            }
          }
        );
      });
    },
    //物资成品入库验收Table确认审核----------------------接口
    postApiGoodsHandaworkCheckstockbillApi(id) {
      this.$cu.showLoading();
      return new Promise((resolve) => {
        postApiGoodsHandaworkCheckstockbill({ postockid: id }).then((res) => {
          this.$cu.hideLoading();
          if (res.retCode === 2000) {
            this.$Message.success(res.retMsg);
            this.getDishStockListApi();
            resolve();
          }
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
